package com.qys.livesMall.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.DateUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.system.model.dto.sys.SysPaymentPlatformDTO;
import com.qys.livesMall.system.model.entity.H2Channel;
import com.qys.livesMall.system.model.entity.SysPaymentPlatform;
import com.qys.livesMall.system.mapper.SysPaymentPlatformMapper;
import com.qys.livesMall.system.model.vo.sys.SysPaymentPlatformVO;
import com.qys.livesMall.system.service.SysH2ChannelService;
import com.qys.livesMall.system.service.SysPaymentPlatformService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 支付平台 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-11-06
 */
@Service
public class SysPaymentPlatformServiceImpl extends ServiceImpl<SysPaymentPlatformMapper, SysPaymentPlatform> implements SysPaymentPlatformService {

    @Resource
    private SysH2ChannelService sysH2ChannelService;
    @Override
    public IPage<SysPaymentPlatformVO> queryPage(BaseQuery query) {
        List<H2Channel> channelList= sysH2ChannelService.list();
        Map<Integer,H2Channel> map=channelList.stream().collect(Collectors.toMap(paramCalcrate -> paramCalcrate.getId(), Function.identity()));

        LambdaQueryWrapper<SysPaymentPlatform> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPaymentPlatform::getDeleted,0);
        queryWrapper.orderByDesc(SysPaymentPlatform::getCreateTime);
        IPage<SysPaymentPlatformVO> page=page(new Page<>(query.getPageNum(), query.getPageSize()), queryWrapper).convert(e -> {
            SysPaymentPlatformVO paymentPlatformVO=new SysPaymentPlatformVO();
            BeanUtil.copyProperties(e,paymentPlatformVO);
            H2Channel h2Channel=map.get(paymentPlatformVO.getChannelId());
            if (null != h2Channel){
                paymentPlatformVO.setChannelName(h2Channel.getChannelName());
            }
            return paymentPlatformVO;
        });
        return page;
    }

    @Override
    public boolean add(SysPaymentPlatformDTO dto) {
        dto.setId(null);
        SysPaymentPlatform sysPaymentPlatform=new SysPaymentPlatform();
        BeanUtil.copyProperties(dto,sysPaymentPlatform);
        sysPaymentPlatform.setCreateTime(DateUtils.dateTimeNow());
        sysPaymentPlatform.setDeleted((byte) 0);
        sysPaymentPlatform.setState(0);
        return save(sysPaymentPlatform);
    }

    @Override
    public boolean edit(SysPaymentPlatformDTO dto) {
        SysPaymentPlatform sysPaymentPlatform=getById(dto.getId());
        if (null == sysPaymentPlatform){
            return false;
        }
        BeanUtil.copyProperties(dto,sysPaymentPlatform);
        return updateById(sysPaymentPlatform);
    }
}
